package com.kingyea.mobilepolice.apimanage.interfaceflow.controller;

import com.kingyea.mobilepolice.apimanage.interfaceflow.service.InterfaceFlowService;
import com.kingyea.mobilepolice.apimanage.sysvo.bean.AllCondition;
import com.kingyea.mobilepolice.apimanage.utils.ExceptionConditions;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author cxc
 * @version v1.0.0
 * @description
 * @date Created in 2018-05-05 21:01
 */
@RequestMapping(value = "/interfaceFlow")
@RestController
public class InterfaceFlowController {

    private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceFlowController.class);

    @Autowired
    private InterfaceFlowService interfaceFlowService;

    /**
     * @Description: 查询请求方和提供方的流量
     * @param: [condition]
     * @return: java.lang.Object
     * @author: cxc
     * @Date: 2018-05-06
     */
    @PostMapping("/search")
    public Object search(@RequestBody AllCondition condition) throws Exception {
        ExceptionConditions.checkNotNull(condition.getType(), StateCode.NO_ARGUMENT_ERROR);
//        ExceptionConditions.checkNotNull(condition.getStartTime(), StateCode.NO_ARGUMENT_ERROR);
//        //只允许查询30天前的数据，包括第30天
//        ExceptionConditions.checkArgument(!LocalDate.now().minusDays(30).isAfter(condition.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()), StateCode.ILLEGAL_ARGUMENT_ERROR);
//        ExceptionConditions.checkNotNull(condition.getEndTime(), StateCode.NO_ARGUMENT_ERROR);
        //空白字符串转空引用
        condition.setTypeName(Optional.ofNullable(condition.getTypeName()).filter(StringUtils::isNotBlank).map(String::trim).orElse(null));
        condition.setServiceName(Optional.ofNullable(condition.getServiceName()).filter(StringUtils::isNotBlank).map(String::trim).orElse(null));
        condition.setUrl(Optional.ofNullable(condition.getUrl()).filter(StringUtils::isNotBlank).map(String::trim).orElse(null));
        return interfaceFlowService.search(condition);
    }

    /**
     * @Description: 请求流量汇总
     * @param: [condition]
     * @return: java.lang.Object
     * @author: cxc
     * @Date: 2018-05-07
     */
    @PostMapping("/searchGather")
    public Object searchGather(@RequestBody AllCondition condition) throws Exception {
        ExceptionConditions.checkNotNull(condition.getType(), StateCode.NO_ARGUMENT_ERROR);
        condition.setPageNum(Optional.of(condition.getPageNum()).filter(num -> num > 0).orElse(1));
        condition.setPageSize(Optional.of(condition.getPageSize()).filter(size -> size > 0).orElse(5));
//        ExceptionConditions.checkNotNull(condition.getStartTime(), StateCode.NO_ARGUMENT_ERROR);
//        //只允许查询30天前的数据，包括第30天
//        ExceptionConditions.checkArgument(!LocalDate.now().minusDays(30).isAfter(condition.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()), StateCode.ILLEGAL_ARGUMENT_ERROR);
        return interfaceFlowService.searchGather(condition);
    }

}
